package cn.doitedu.dw_utils

import scala.collection.immutable

object AttributeUtils {


  /**
   * 时间衰减归因算法
   * @param events
   * @return
   *
   * [(1,e1,100000),(1,e2,100002),(1,e3,100003)]
   * 1              2             3
   *
   */
  def timeDecayAttribute(events:List[(Long,String,Long)]): List[(Long,String,Long,Double)] ={

    // 求list的各元素的权重（index+1）总和
    // [1,2,3]
    val factors: List[Int] = events.indices.map(_ + 1).toList

    val factorSum = factors.sum

    events.zip(factors).map(tp=>{
      val element: (Long, String, Long) = tp._1
      val factor = tp._2

      (element._1,element._2,element._3,100.0*factor/factorSum)

    })
  }


  /**
   * 首次触点归因算法
   * @param events
   * @return
   */
  def firstEventAttribute(events:List[(Long,String,Long)]): (Long,String,Long,Double) ={
    val head: (Long, String, Long) = events.head
    (head._1,head._2,head._3,100)
  }


  /**
   *  末次触点归因算法
   * @param events
   * @return
   */
  def lastEventAttribute(events:List[(Long,String,Long)]): (Long,String,Long,Double) ={
    firstEventAttribute(events.reverse)
  }


  /**
   *  线性归因算法
   * @param events
   * @return
   */
  def linearAttribute(events:List[(Long,String,Long)]): List[(Long,String,Long,Double)] ={
    val size: Int = events.size
    events.map(tp=>{
      (tp._1,tp._2,tp._3,100.0/size)
    })

  }



}
